import json
from loguru import logger
import pandas as pd
from .read_table import input_table
from .manage_table import manage_table
from .crud_tabe import  crud_table
from .join_table import join_table


def excel_json(fliepath):
    # 结果
    logger.info(f"读取excel文件:{fliepath}")
    result = []
    excel_file = pd.ExcelFile(fliepath)
    sheet_names = excel_file.sheet_names
    for sheet_name in sheet_names:
        sheet_data = excel_file.parse(sheet_name,header=None).values.tolist()
        
        for row_index,rows in enumerate(sheet_data):
            # print(rows)
            for column_index  ,value in  enumerate(rows):
                data_strip =str(value).strip()
                if  data_strip.startswith('{'):
                    # print(data_strip)
                    try:
                        
                        result.append(json.loads(data_strip))
                        
                    except json.decoder.JSONDecodeError as e:
                        logger.error(f"<{sheet_name}>.[{row_index},{column_index}]{e}")
    return result

def run(fliepath):
    logger.info(f"读取excel文件:{fliepath}")
    
    excel_file = pd.ExcelFile(fliepath)
    sheet_names = excel_file.sheet_names
    for sheet_name in sheet_names:
        sheet_data = excel_file.parse(sheet_name,header=None).values.tolist()
        for row_index,rows in enumerate(sheet_data):
            for column_index  ,value in  enumerate(rows):
                data_strip =str(value).strip()
                if  data_strip.startswith('{'):
                    logger.info(f"正在解析:<{sheet_name}>.[{row_index},{column_index}]")
                    
                    try:
                        activeinfo=json.loads(data_strip)
                        if activeinfo['action'] == 'input':
                            input_table(activeinfo)
                        elif activeinfo['action'] == 'manage':
                            manage_table(activeinfo)
                        elif activeinfo['action'] == 'crud':
                            crud_table(activeinfo)
                        elif activeinfo['action'] == 'join':
                            join_table(activeinfo)
                        
                        
                    except json.decoder.JSONDecodeError as e:
                        logger.error(f"解析json错误:<{sheet_name}>.[{row_index},{column_index}]{e}")
                        return f"解析json错误:<{sheet_name}>.[{row_index},{column_index}]{e}"
                    except Exception as e:
                        logger.error(f"计算错误: <{sheet_name}>.[{row_index},{column_index}]{e}")
                        return f"计算错误: <{sheet_name}>.[{row_index},{column_index}]{e}"
                    
                        
    
    
    # excel_lists=excel_json(fliepath)
    # for activeinfo in excel_lists:
    #     if activeinfo['action'] == 'input':
    #         input_table(activeinfo)
    #     elif activeinfo['action'] == 'manage':
    #         manage_table(activeinfo)
    #     elif activeinfo['action'] == 'crud':
    #         crud_table(activeinfo)
    #     elif activeinfo['action'] == 'join':
    #         join_table(activeinfo)
        
    
